/**
  * 绑定银行卡
  */
module game {

    export class BankCardBindMediator extends BaseMediator {
        public static NAME: string = "BankCardBindMediator";
        public constructor(viewComponent: any = null) {
            super(BankCardBindMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_BANK_CARD_BIND,
                PanelNotify.HALL.OPEN_BANK_CARD_BIND_MODIFU,
                PanelNotify.HALL.CLOSE_BANK_CARD_BIND
            ];
        }
        private panel: BankCardBindPanel;
        public handleNotification(notification: puremvc.INotification): void {

            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_BANK_CARD_BIND: {
                    this.panel = null;
                    this.panel = new BankCardBindPanel();
                    this.showUI(this.panel, 2, 1, true);
                    this.panel.codeGroup.visible = false
                    this.panel.confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.confirm, this);
                    break;
                }
                case PanelNotify.HALL.OPEN_BANK_CARD_BIND_MODIFU: {
                    this.panel = null;
                    this.panel = new BankCardBindPanel();
                    this.showUI(this.panel, 2, 1, true);
                    this.modifyBank();
                    this.panel.input_name.text = notification.getBody();
                    this.panel.codeGroup.visible = true
                    this.panel.confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.confirmModify, this);
                    break;
                }
                case PanelNotify.HALL.CLOSE_BANK_CARD_BIND: {
                    egret.clearInterval(this.timeOut)
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this);
            this.panel.selectBankGroup.addEventListener(egret.TouchEvent.TOUCH_TAP, this.openBankNameSelect, this);
            this.panel.getCode.addEventListener(egret.TouchEvent.TOUCH_TAP, this.getCodeClick, this);
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            this.panel.selectBankVive.visible = false;
            this.panel.BankNameList.removeChildren()
            var bankNameArr = [
                "中国银行",
                "工商银行",
                "农业银行",
                "交通银行",
                "建设银行",
                "招商银行",
                "兴业银行",
                "民生银行",
                "平安银行",
                "中信银行",
                "浦发银行",
                "光大银行",
                "邮政银行",
                "华夏银行",
                "宁波银行",
                "广发银行",
                "南京银行",
                "北京银行",
                "上海银行",
                "北京农商银行",
                "上海浦东发展银行"
            ]
            for (var i = 0; i < bankNameArr.length; i++) {
                var bankName = new eui.Label(bankNameArr[i])
                bankName.textAlign = "center"
                bankName.verticalAlign = "middle"
                bankName.border = true
                bankName.borderColor = 0xFFFFFF
                this.panel.BankNameList.addChild(bankName);
                bankName.addEventListener(egret.TouchEvent.TOUCH_TAP, this.bankSelect, this)
            }
            this.panel.selectedBankName.text = bankNameArr[0];
        }
        //修改银行卡，这时候用户名禁止输入，亮起验证码输入框
        private modifyBank() {
            this.panel.input_name.touchEnabled = false;
            this.panel.codeGroup.visible = true;
        }
        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            egret.clearInterval(this.timeOut)
            this.closePanel(1);
        }
        //打开银行选择列表
        private openBankNameSelect(eve: egret.TouchEvent) {
            EffectUtils.playDim(eve.currentTarget)
            if (this.panel.selectBankVive.visible == true) {
                this.panel.selectBankVive.visible = false;
            } else {
                this.panel.selectBankVive.visible = true;
            }
        }
        //选择银行名字
        private bankSelect(eve: egret.TouchEvent) {
            var lab: eui.Label = eve.currentTarget;
            this.panel.selectedBankName.text = lab.text;
            this.panel.selectBankVive.visible = false;
        }
        //提交绑定银行
        private confirm() {
            var userName = this.panel.input_name.text
            var bankCar = this.panel.input_bankCard.text
            var bankCar2 = this.panel.input_bankCard2.text
            var bankName = this.panel.selectedBankName.text
            if (RegUtils.isNull(userName)) {
                Global.tipShow("请输入正确的用户名")
                return;
            }
            if (RegUtils.isNull(bankName)) {
                Global.tipShow("请选择正确的银行名")
                return;
            }
            if (!RegUtils.isInteger(bankCar)) {
                Global.tipShow("请输入12-20位数字的银行卡号")
                return;
            }
            if (bankCar.length < 12 || bankCar.length > 20) {
                Global.tipShow("请输入12-20位数字的银行卡号")
                return;
            }
            if (bankCar2 != bankCar) {
                Global.tipShow("两次输入的银行卡号不一致！")
                return;
            }


            var time = Global.getCurrTimeLong()
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/GameWebApplication/PayBindCard.ashx?userid=" + GameVO.getVo().dwUserID + "&signature=" + signature + "&time=" + time, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        EffectUtils.textFadeAway("银行卡绑定成功!")
                        this.closePanel(0)
                        game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_TIXIAN)
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            var obj = {
                "Bank": bankName,
                "BankAccountType": 0,
                "AccountNo": bankCar,
                "UserId": GameVO.getVo().dwUserID,
                "BankAccountUsername": userName
            }
            EffectUtils.showWaritPanel()
            request.send(JSON.stringify(obj));
        }
        //提交更换银行
        private confirmModify() {
            var userName = this.panel.input_name.text
            var code = this.panel.yanZhengMa.text
            var bankCar = this.panel.input_bankCard.text
            var bankCar2 = this.panel.input_bankCard2.text
            var bankName = this.panel.selectedBankName.text
            if (RegUtils.isNull(userName)) {
                Global.tipShow("请输入正确的用户名")
                return;
            }
            if (RegUtils.isNull(code)) {
                Global.tipShow("请输入验证码")
                return;
            }
            if (RegUtils.isNull(bankName)) {
                Global.tipShow("请选择正确的银行名")
                return;
            }
            if (!RegUtils.isInteger(bankCar)) {
                Global.tipShow("请输入12-20位数字的银行卡号")
                return;
            }
            if (bankCar.length < 12 || bankCar.length > 20) {
                Global.tipShow("请输入12-20位数字的银行卡号")
                return;
            }
            if (bankCar2 != bankCar) {
                Global.tipShow("两次输入的银行卡号不一致！")
                return;
            }


            var time = Global.getCurrTimeLong()
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/GameWebApplication/ResetBankInfo.ashx?userid=" + GameVO.getVo().dwUserID + "&signature=" + signature + "&time=" + time, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        EffectUtils.textFadeAway("银行卡更换成功!")
                        egret.clearInterval(this.timeOut)
                        this.closePanel(1)
                        game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_TIXIAN)
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            var obj = {
                "Bank": bankName,
                PhoneCode: code,
                "Type": "0",
                "AccountNo": bankCar,
                "UserId": GameVO.getVo().dwUserID
            }
            EffectUtils.showWaritPanel()
            request.send(JSON.stringify(obj));
        }
        //获取验证码
        private getCodeClick() {
            var time = Global.getCurrTimeLong()
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/GameWebApplication/GetPhoneCode.ashx?userid=" + GameVO.getVo().dwUserID + "&signature=" + signature + "&time=" + time, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        EffectUtils.textFadeAway("获取验证码成功!")
                        this.panel.getCode.touchEnabled = false;
                        this.panel.getCode.icon = "tixian_hqyzm_h"
                        this.panel.getCode.label = "重新获取60";
                        this.GetCodeTime();
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            var obj = {
                OptionType: 2,
                PhoneNum: GameVO.getVo().szMobilePhone,
                CountryCode: "86"
            }
            EffectUtils.showWaritPanel()
            request.send(JSON.stringify(obj));
        }
        //重新获取验证码倒计时
        private timeOut = null
        private GetCodeTime() {
            egret.clearInterval(this.timeOut)
            var num = 60
            this.timeOut = egret.setInterval(() => {
                num--;
                this.panel.getCode.label = "重新获取" + num;
                if (num <= 0) {
                    this.panel.getCode.touchEnabled = true;
                    this.panel.getCode.icon = "tixian_hqyzm"
                    this.panel.getCode.label = ""
                    egret.clearInterval(this.timeOut)
                }
            }, this, 1000)

        }
    }
}